﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace BPMS.Core.DataAccess
{
    public class DbParameterProvider
    {
        private readonly DbProviderFactory ProviderFactory;

        public DbParameterProvider(DbProviderFactory providerFactory)
        {
            this.ProviderFactory = providerFactory;
        }

        public DbParameter[] GetParameterCollection(IEnumerable<DbParameterWrapper> parameters)
        {
            List<DbParameter> collection = new List<DbParameter>();
            foreach (DbParameterWrapper item in parameters)
            {
                collection.Add(CreateParameter(item));
            }
            return collection.ToArray();
        }

        public DbParameter CreateParameter(DbParameterWrapper parameterWrapper)
        {
            DbParameter parameter = ProviderFactory.CreateParameter();
            parameter.ParameterName = parameterWrapper.ParameterName;
            parameter.Value = parameterWrapper.ParameterValue ?? DBNull.Value;
            parameter.Direction = parameterWrapper.Direction;
            if (parameterWrapper.ParameterSize > 0)
            {
                parameter.Size = parameterWrapper.ParameterSize;
            }
            return parameter;
        }
    }
}
